#include "Map.h"
#include "..\\SGD Wrappers\\SGD_GraphicsManager.h"
#include "..\\SGD Wrappers\\SGD_EventManager.h"
#include "..\\SGD Wrappers\\SGD_Event.h"
#include "GameObject.h"
#include "StaticObject.h"
#include "..\\SGD Wrappers\\SGD_MessageManager.h"
#include "CreatePlayerMessage.h"
#include "CreatePortalMessage.h"
#include "CreateGoblinMessage.h"
#include "CreateElfMessage.h"
#include "CreateBatMessage.h"
#include "CreateKnightMessage.h"
#include "CreateArcherMessage.h"
#include "CreateSpiderMessage.h"
#include "CreateOgreMessage.h"
#include "CreateGiantSpiderMessage.h"
#include "CreateVampireMessage.h"
#include "CreateDragonMessage.h"
#include "CreateAugmentMessage.h"
#include "CreateLadderMessage.h"
#include "CreateStairsMessage.h"
#include "CreateWeaponAugmentTable.h"
#include "GameplayState.h"
#include "Player.h"
#include "MovingObject.h"
#include "Projectile.h"
void Map::LoadTileSet(const char * tSet)
{
	tileset = SGD::GraphicsManager::GetInstance()->LoadTexture(tSet);
}
void Map::UnloadTileSet()
{
	SGD::GraphicsManager::GetInstance()->UnloadTexture(tileset);
}

Map::Map()
{
	graphics = SGD::GraphicsManager::GetInstance();
}
void Map::Render(SGD::Point camera, float screenWidth, float screenHeight)
{

	for (int c = int(camera.x / tileSize.width); c < mapSize.width && c < (screenWidth / tileSize.width) + (camera.x / tileSize.width); c++)
	{
		for (int r = int(camera.y / tileSize.height); r < mapSize.height && r < (screenHeight / tileSize.height) + (camera.y / tileSize.height); r++)
		{
			for (int l = 0; l < numLayers; l++)
			{
				Tile& tile = layers[l].tiles[unsigned int(mapSize.width * r + c)];
				if (tile.activeRender)
				{
					SGD::Rectangle srcRec;
					srcRec.left = tile.tsPos.x * tileSize.width;
					srcRec.top = tile.tsPos.y * tileSize.height;
					srcRec.right = srcRec.left + tileSize.width;
					srcRec.bottom = srcRec.top + tileSize.height;

					SGD::Point position;
					position.x = tile.mapPos.left - camera.x;
					position.y = tile.mapPos.top - camera.y;

					graphics->DrawTextureSection(tileset, position, srcRec);
				}
			}
		}
	}
	collisionRect.left -= camera.x;
	collisionRect.top -= camera.y;
	collisionRect.right -= camera.x;
	collisionRect.bottom -= camera.y;

	//graphics->DrawRectangle(collisionRect, SGD::Color(128, 0, 0, 255) );
}
void Map::CheckCollision(GameObject* object)
{
	if (object != nullptr)
	{
		MovingObject* mObject = nullptr;
		StaticObject* sObject = nullptr;
		if (mObject = dynamic_cast<MovingObject*>(object))
		{
			if (mObject->GetPosition().x < 0)
			{
				if (mObject->GetType() == MovingObject::OBJ_BULLET)
				{
					BulletDeletion(dynamic_cast<Projectile*>(mObject));
				}
				else
					mObject->SetPosition(SGD::Point(mObject->GetRect().ComputeWidth() * .5f, mObject->GetPosition().y));
			}
			else if (mObject->GetPosition().x > mapSize.width * tileSize.width)
			{
				if (mObject->GetType() == MovingObject::OBJ_BULLET)
				{
					BulletDeletion(dynamic_cast<Projectile*>(mObject));
				}
				else
					mObject->SetPosition(SGD::Point((mapSize.width * tileSize.width) - (mObject->GetRect().ComputeWidth() * .5f), mObject->GetPosition().y));
			}

			if (mObject->GetPosition().y < 0)
			{
				if (mObject->GetType() == MovingObject::OBJ_BULLET)
				{
					BulletDeletion(dynamic_cast<Projectile*>(mObject));
				}
				else
					mObject->SetPosition(SGD::Point(mObject->GetPosition().x, mObject->GetRect().ComputeHeight()));
			}
			else if (mObject->GetPosition().y > mapSize.height * tileSize.height)
			{
				if (mObject->GetType() == MovingObject::OBJ_BULLET)
				{
					BulletDeletion(dynamic_cast<Projectile*>(mObject));
				}
				else
					mObject->SetPosition(SGD::Point(mObject->GetPosition().x, (mapSize.height * tileSize.height)));
			}

			int col;
			int row;
			SGD::Rectangle intersection;
			
			for (int iteration = 0; iteration < 3; iteration++)
			{
				col = int(mObject->GetRect().ComputeCenter().x) / int(tileSize.width);
				if (col < 0)
					col = 0;
				if (col >= int(mapSize.width))
					col = int(mapSize.width - 1);
				row = int(mObject->GetRect().bottom) / int(tileSize.height);
				if (row < 0)
					row = 0;
				if (row >= int(mapSize.height))
					row = int(mapSize.height - 1);

				for (int lay = 0; lay < numLayers; lay++)
				{
					Tile* tile = &layers[lay].tiles[unsigned int(mapSize.width * row + col)];
					if (!tile->turnedoff && tile->isCollidable && tile->type != "Boss" && !(mObject->GetType() == MovingObject::OBJ_BULLET && tile->type == "Platform"))
					{
						intersection = mObject->GetRect().ComputeIntersection(tile->mapPos);

						if (mObject->GetType() == MovingObject::OBJ_BULLET)
						{
							BulletDeletion(dynamic_cast<Projectile*>(mObject));
						}
						else if (tile->type == "Boss" && mObject->GetType() == MovingObject::OBJ_PLAYER)
						{
							dynamic_cast<Player*>(mObject)->SetMinPosInWorld(tile->mapPos.right);
						}
						else if (tile->type == "Lava" && mObject->GetType() == MovingObject::OBJ_PLAYER)
						{
							if (!mObject->GetDamageFlash())
							{
								mObject->SetHealth(mObject->GetHealth() - 50);
								mObject->SetDamageFlash(true);
								mObject->SetFlashDuration(1.0f);
							}
							mObject->SetVelocity(SGD::Vector(mObject->GetVelocity().x, mObject->GetVelocity().y - 700.0f));
						}
						else
						{
							mObject->SetPosition({ mObject->GetPosition().x, intersection.top });
							mObject->SetVelocity({ mObject->GetVelocity().x, 0 });
							mObject->SetGravityState(false);
						}
					}
				}
			}

			for (int iteration = 0; iteration < 3; iteration++)
			{
				col = int(mObject->GetRect().left) / int(tileSize.width);
				if (col < 0)
					col = 0;
				if (col >= int(mapSize.width))
					col = int(mapSize.width - 1);
				row = int(mObject->GetRect().bottom - (mObject->GetRect().ComputeWidth() / 3)) / int(tileSize.height);
				if (row < 0)
					row = 0;
				if (row >= int(mapSize.height))
					row = int(mapSize.height - 1);

				for (int lay = 0; lay < numLayers; lay++)
				{
					Tile* tile = &layers[lay].tiles[unsigned int(mapSize.width * row + col)];
					intersection = mObject->GetRect().ComputeIntersection(tile->mapPos);
					if (!tile->turnedoff && tile->isCollidable && tile->type != "Boss" && tile->type != "Platform" && tile->type != "Lava")
					{
						if (mObject->GetType() == MovingObject::OBJ_BULLET)
						{
							BulletDeletion(dynamic_cast<Projectile*>(mObject));
						}
						else
						{
							mObject->SetPosition({ intersection.right + (mObject->GetRect().ComputeWidth() * .5f), mObject->GetPosition().y });
							mObject->SetVelocity({ 0, mObject->GetVelocity().y });
						}
					}
					else if (tile->type == "Boss" && mObject->GetType() == MovingObject::OBJ_PLAYER)
					{
						dynamic_cast<Player*>(mObject)->SetMinPosInWorld(tile->mapPos.right);
					}
				}
			}

			for (int iteration = 0; iteration < 3; iteration++)
			{
				col = int(mObject->GetRect().left) / int(tileSize.width);
				if (col < 0)
					col = 0;
				if (col >= int(mapSize.width))
					col = int(mapSize.width - 1);
				row = int(mObject->GetRect().top + (mObject->GetRect().ComputeWidth() / 3)) / int(tileSize.height);
				if (row < 0)
					row = 0;
				if (row >= int(mapSize.height))
					row = int(mapSize.height - 1);

				for (int lay = 0; lay < numLayers; lay++)
				{
					Tile* tile = &layers[lay].tiles[unsigned int(mapSize.width * row + col)];
					intersection = mObject->GetRect().ComputeIntersection(tile->mapPos);
					if (!tile->turnedoff && tile->isCollidable && tile->type != "Boss" && tile->type != "Platform" && tile->type != "Lava")
					{
						if (mObject->GetType() == MovingObject::OBJ_BULLET)
						{
							BulletDeletion(dynamic_cast<Projectile*>(mObject));
						}
						else
						{
							mObject->SetPosition({ intersection.right + (mObject->GetRect().ComputeWidth() * .5f), mObject->GetPosition().y });
							mObject->SetVelocity({ 0, mObject->GetVelocity().y });
						}
					}
					else if (tile->type == "Boss" && mObject->GetType() == MovingObject::OBJ_PLAYER)
					{
						dynamic_cast<Player*>(mObject)->SetMinPosInWorld(tile->mapPos.right);
					}
				}
			}

			for (int iteration = 0; iteration < 3; iteration++)
			{
				col = int(mObject->GetRect().right) / int(tileSize.width);
				if (col < 0)
					col = 0;
				if (col >= int(mapSize.width))
					col = int(mapSize.width - 1);
				row = int(mObject->GetRect().bottom - (mObject->GetRect().ComputeWidth() / 3)) / int(tileSize.height);
				if (row < 0)
					row = 0;
				if (row >= int(mapSize.height))
					row = int(mapSize.height - 1);

				for (int lay = 0; lay < numLayers; lay++)
				{
					Tile* tile = &layers[lay].tiles[unsigned int(mapSize.width * row + col)];
					intersection = mObject->GetRect().ComputeIntersection(tile->mapPos);
					if (!tile->turnedoff && tile->isCollidable && tile->type != "Boss" && tile->type != "Platform" && tile->type != "Lava")
					{
						if (mObject->GetType() == MovingObject::OBJ_BULLET)
						{
							BulletDeletion(dynamic_cast<Projectile*>(mObject));
						}
						else
						{
							mObject->SetPosition({ intersection.left - (mObject->GetRect().ComputeWidth() * .5f), mObject->GetPosition().y });
							mObject->SetVelocity({ 0, mObject->GetVelocity().y });
						}
					}
				}
			}

			for (int iteration = 0; iteration < 3; iteration++)
			{
				col = int(mObject->GetRect().right) / int(tileSize.width);
				if (col < 0)
					col = 0;
				if (col >= int(mapSize.width))
					col = int(mapSize.width - 1);
				row = int(mObject->GetRect().top + (mObject->GetRect().ComputeWidth() / 3)) / int(tileSize.height);
				if (row < 0)
					row = 0;
				if (row >= int(mapSize.height))
					row = int(mapSize.height - 1);

				for (int lay = 0; lay < numLayers; lay++)
				{
					Tile* tile = &layers[lay].tiles[unsigned int(mapSize.width * row + col)];
					intersection = mObject->GetRect().ComputeIntersection(tile->mapPos);
					if (!tile->turnedoff && tile->isCollidable && tile->type != "Boss" && tile->type != "Platform" && tile->type != "Lava")
					{
						if (mObject->GetType() == MovingObject::OBJ_BULLET)
						{
							BulletDeletion(dynamic_cast<Projectile*>(mObject));
						}
						else
						{
							mObject->SetPosition({ intersection.left - (mObject->GetRect().ComputeWidth() * .5f), mObject->GetPosition().y });
							mObject->SetVelocity({ 0, mObject->GetVelocity().y });
						}
					}
				}
			}

			for (int iteration = 0; iteration < 3; iteration++)
			{
				col = int(mObject->GetRect().ComputeCenter().x) / int(tileSize.width);
				if (col < 0)
					col = 0;
				if (col > int(mapSize.width))
					col = int(mapSize.width - 1);
				row = int(mObject->GetRect().top) / int(tileSize.height);
				if (row < 0)
					row = 0;
				if (row >= int(mapSize.height))
					row = int(mapSize.height - 1);

				for (int lay = 0; lay < numLayers; lay++)
				{
					Tile* tile = &layers[lay].tiles[unsigned int(mapSize.width * row + col)];
					intersection = mObject->GetRect().ComputeIntersection(tile->mapPos);
					if (!tile->turnedoff && tile->isCollidable && tile->type != "Boss" && tile->type != "Platform" && tile->type != "Lava")
					{
						if (mObject->GetType() == MovingObject::OBJ_BULLET)
						{
							BulletDeletion(dynamic_cast<Projectile*>(mObject));
						}
						else
						{
							mObject->SetPosition({ mObject->GetPosition().x, intersection.bottom + mObject->GetRect().ComputeHeight() });
							mObject->SetVelocity({ mObject->GetVelocity().x, 0 });
						}

					}
					else if (tile->type == "Boss" && mObject->GetType() == MovingObject::OBJ_PLAYER)
					{
						dynamic_cast<Player*>(mObject)->SetMinPosInWorld(tile->mapPos.right);
					}
				}
			}
		}
		else
		{
			sObject = dynamic_cast<StaticObject*>(object);

			for (int i = 0; i < numLayers; i++)
			{
				Layer l = layers[i];
				int row = int(sObject->GetRect().bottom) / int(tileSize.height);

				for (int col = int(sObject->GetRect().left) / int(tileSize.width); col <= int(sObject->GetRect().right) / int(tileSize.width); col++)
				{
					Tile t = l.tiles[unsigned int(mapSize.width * row + col)];
					SGD::Rectangle intersection = sObject->GetRect().ComputeIntersection(t.mapPos);

					if (!t.turnedoff &&	!intersection.IsEmpty() && t.isCollidable &&
						sObject->GetRect().bottom >= intersection.top &&
						sObject->GetRect().bottom <= intersection.bottom)
					{
						sObject->SetPosition({ sObject->GetPosition().x, intersection.top - 1 });
						sObject->SetVelocity({ 0, 0 });
						sObject->SetGravityState(false);
						return;
					}
				}
			}
		}
	}
}

bool Map::CheckCollision(SGD::Point position)
{
	for (int i = 0; i < numLayers; i++)
	{
		if (tileSize == SGD::Size{})
			continue;
		int row = ((int)position.y / (int)tileSize.height);
		int col = ((int)position.x / (int)tileSize.width);

		if (row < 0 || col < 0) return true;
		if (row >= mapSize.height || col >= mapSize.width) return true;

		Tile& t = layers[i].tiles[(int)mapSize.width * row + col];
		if ((t.isCollidable && t.type != "Lava") || t.type == "Climbing" || t.type == "Stairs")
		{
			return true;
		}
	}
	return false;
}

void Map::TurnOffTile(SGD::Point point)
{
	for (int i = 0; i < numLayers; i++)
	{
		Layer& l = layers[i];
		Tile& t = l.tiles[(int)mapSize.width * ((int)point.y / (int)tileSize.height) + ((int)point.x / (int)tileSize.width)];
		if (t.type == "Ground")
		{
			t.activeRender = false;
			t.turnedoff = true;
		}
	}
}

void Map::Start()
{
	for (int i = 0; i < numLayers; i++)
	{
		for (int col = 0; col < int(mapSize.width); col++)
		{
			for (int row = 0; row < int(mapSize.height); row++)
			{
				if (layers[i].tiles[unsigned int(mapSize.width * row + col)].turnedoff)
				{
					layers[i].tiles[unsigned int(mapSize.width * row + col)].activeRender = true;
					layers[i].tiles[unsigned int(mapSize.width * row + col)].turnedoff = false;
				}
				else if (!layers[i].tiles[unsigned int(mapSize.width * row + col)].triggered)
				{
					if (strcmp(layers[i].tiles[unsigned int(mapSize.width * row + col)].event.c_str(), "") == 0 ||
						strcmp(layers[i].tiles[unsigned int(mapSize.width * row + col)].event.c_str(), "NONE") == 0 ||
						strcmp(layers[i].tiles[unsigned int(mapSize.width * row + col)].event.c_str(), "TILECOLLISION") == 0)
					{
						continue;
					}
					else if (strcmp(layers[i].tiles[unsigned int(mapSize.width * row + col)].event.c_str(), "SPAWN_PLAYER") == 0)
					{
						if (GameplayState::GetInstance()->GetPlayer() == nullptr)
						{
							SGD::MessageManager::GetInstance()->QueueMessage(new CreatePlayerMessage(col * tileSize.width, row * tileSize.height + tileSize.height));
						}
						else
						{
							GameplayState::GetInstance()->GetPlayer()->SetPosition(SGD::Point(col * tileSize.width, row * tileSize.height + tileSize.height));
						}
					}
					else if (strcmp(layers[i].tiles[unsigned int(mapSize.width * row + col)].event.c_str(), "SPAWN_BASE_PORTAL") == 0)
					{
						SGD::MessageManager::GetInstance()->QueueMessage(new CreatePortalMessage(0, col * tileSize.width, row * tileSize.height + tileSize.height));
					}
					else if (strcmp(layers[i].tiles[unsigned int(mapSize.width * row + col)].event.c_str(), "SPAWN_LEVEL1_PORTAL") == 0)
					{
						SGD::MessageManager::GetInstance()->QueueMessage(new CreatePortalMessage(1, col * tileSize.width, row * tileSize.height + tileSize.height));
					}
					else if (strcmp(layers[i].tiles[unsigned int(mapSize.width * row + col)].event.c_str(), "SPAWN_LEVEL2_PORTAL") == 0)
					{
						SGD::MessageManager::GetInstance()->QueueMessage(new CreatePortalMessage(2, col * tileSize.width, row * tileSize.height + tileSize.height));
					}
					else if (strcmp(layers[i].tiles[unsigned int(mapSize.width * row + col)].event.c_str(), "SPAWN_LEVEL3_PORTAL") == 0)
					{
						SGD::MessageManager::GetInstance()->QueueMessage(new CreatePortalMessage(3, col * tileSize.width, row * tileSize.height + tileSize.height));
					}
					else if (strcmp(layers[i].tiles[unsigned int(mapSize.width * row + col)].event.c_str(), "SPAWN_LEVEL4_PORTAL") == 0)
					{
						SGD::MessageManager::GetInstance()->QueueMessage(new CreatePortalMessage(4, col * tileSize.width, row * tileSize.height + tileSize.height));
					}
					else if (strcmp(layers[i].tiles[unsigned int(mapSize.width * row + col)].event.c_str(), "SPAWN_UCC_PORTAL") == 0)
					{
						SGD::MessageManager::GetInstance()->QueueMessage(new CreatePortalMessage(col * tileSize.width, row * tileSize.height + tileSize.height));
					}
					else if (strcmp(layers[i].tiles[unsigned int(mapSize.width * row + col)].event.c_str(), "SPAWN_WARP_PORTAL") == 0)
					{
						SGD::MessageManager::GetInstance()->QueueMessage(new CreatePortalMessage(col * tileSize.width, row * tileSize.height + tileSize.height, col * tileSize.width, row * tileSize.height + tileSize.height));
					}
					else if (strcmp(layers[i].tiles[unsigned int(mapSize.width * row + col)].event.c_str(), "SPAWN_LADDER") == 0)
					{
						SGD::MessageManager::GetInstance()->QueueMessage(new CreateLadderMessage(col * tileSize.width, row * tileSize.height + tileSize.height));
					}
					else if (strcmp(layers[i].tiles[unsigned int(mapSize.width * row + col)].event.c_str(), "SPAWN_STAIRS") == 0)
					{
						SGD::MessageManager::GetInstance()->QueueMessage(new CreateStairsMessage(col * tileSize.width, row * tileSize.height + tileSize.height));
					}
					else if (strcmp(layers[i].tiles[unsigned int(mapSize.width * row + col)].event.c_str(), "SPAWN_OGRE") == 0)
					{
						SGD::MessageManager::GetInstance()->QueueMessage(new CreateOgreMessage(col * tileSize.width, row * tileSize.height + tileSize.height));
					}
					else if (strcmp(layers[i].tiles[unsigned int(mapSize.width * row + col)].event.c_str(), "SPAWN_GIANT_SPIDER") == 0)
					{
						SGD::MessageManager::GetInstance()->QueueMessage(new CreateGiantSpiderMessage(col * tileSize.width, row * tileSize.height + tileSize.height));
					}
					else if (strcmp(layers[i].tiles[unsigned int(mapSize.width * row + col)].event.c_str(), "SPAWN_VAMPIRE") == 0)
					{
						SGD::MessageManager::GetInstance()->QueueMessage(new CreateVampireMessage(col * tileSize.width, row * tileSize.height + tileSize.height));
					}
					else if (strcmp(layers[i].tiles[unsigned int(mapSize.width * row + col)].event.c_str(), "SPAWN_DRAGON") == 0)
					{
						SGD::MessageManager::GetInstance()->QueueMessage(new CreateDragonMessage(col * tileSize.width, row * tileSize.height + tileSize.height));
					}
					else if (strcmp(layers[i].tiles[unsigned int(mapSize.width * row + col)].event.c_str(), "SPAWN_DASH_AUGMENT") == 0)
					{
						SGD::MessageManager::GetInstance()->QueueMessage(new CreateAugmentMessage("Dash", col * tileSize.width, row * tileSize.height + tileSize.height));
						layers[i].tiles[unsigned int(mapSize.width * row + col)].triggered = true;
					}
					else if (strcmp(layers[i].tiles[unsigned int(mapSize.width * row + col)].event.c_str(), "SPAWN_BACKDASH_AUGMENT") == 0)
					{
						SGD::MessageManager::GetInstance()->QueueMessage(new CreateAugmentMessage("Back Dash", col * tileSize.width, row * tileSize.height + tileSize.height));
						layers[i].tiles[unsigned int(mapSize.width * row + col)].triggered = true;
					}
					else if (strcmp(layers[i].tiles[unsigned int(mapSize.width * row + col)].event.c_str(), "SPAWN_SLIDING_AUGMENT") == 0)
					{
						SGD::MessageManager::GetInstance()->QueueMessage(new CreateAugmentMessage("Slide", col * tileSize.width, row * tileSize.height + tileSize.height));
						layers[i].tiles[unsigned int(mapSize.width * row + col)].triggered = true;
					}
					else if (strcmp(layers[i].tiles[unsigned int(mapSize.width * row + col)].event.c_str(), "SPAWN_DOUBLEJUMP_AUGMENT") == 0)
					{
						SGD::MessageManager::GetInstance()->QueueMessage(new CreateAugmentMessage("Double Jump", col * tileSize.width, row * tileSize.height + tileSize.height));
						layers[i].tiles[unsigned int(mapSize.width * row + col)].triggered = true;
					}
					else if (strcmp(layers[i].tiles[unsigned int(mapSize.width * row + col)].event.c_str(), "SPAWN_WALLCLIMB_AUGMENT") == 0)
					{
						SGD::MessageManager::GetInstance()->QueueMessage(new CreateAugmentMessage("Wall Climb", col * tileSize.width, row * tileSize.height + tileSize.height));
						layers[i].tiles[unsigned int(mapSize.width * row + col)].triggered = true;
					}
					else if (strcmp(layers[i].tiles[unsigned int(mapSize.width * row + col)].event.c_str(), "SPAWN_ARCANESHOT_AUGMENT") == 0)
					{
						SGD::MessageManager::GetInstance()->QueueMessage(new CreateAugmentMessage("Arcane Shot", col * tileSize.width, row * tileSize.height + tileSize.height));
						layers[i].tiles[unsigned int(mapSize.width * row + col)].triggered = true;
					}
					else if (strcmp(layers[i].tiles[unsigned int(mapSize.width * row + col)].event.c_str(), "SPAWN_ARCANEBEAM_AUGMENT") == 0)
					{
						SGD::MessageManager::GetInstance()->QueueMessage(new CreateAugmentMessage("Arcane Beam", col * tileSize.width, row * tileSize.height + tileSize.height));
						layers[i].tiles[unsigned int(mapSize.width * row + col)].triggered = true;
					}
					else if (strcmp(layers[i].tiles[unsigned int(mapSize.width * row + col)].event.c_str(), "SPAWN_ELECTRONSHOT_AUGMENT") == 0)
					{
						SGD::MessageManager::GetInstance()->QueueMessage(new CreateAugmentMessage("Electron Shot", col * tileSize.width, row * tileSize.height + tileSize.height));
						layers[i].tiles[unsigned int(mapSize.width * row + col)].triggered = true;
					}
					else if (strcmp(layers[i].tiles[unsigned int(mapSize.width * row + col)].event.c_str(), "SPAWN_PHOTONSHOT_AUGMENT") == 0)
					{
						SGD::MessageManager::GetInstance()->QueueMessage(new CreateAugmentMessage("Photon Shot", col * tileSize.width, row * tileSize.height + tileSize.height));
						layers[i].tiles[unsigned int(mapSize.width * row + col)].triggered = true;
					}
					else if (strcmp(layers[i].tiles[unsigned int(mapSize.width * row + col)].event.c_str(), "SPAWN_VAMPIRICMAGNET_AUGMENT") == 0)
					{
						SGD::MessageManager::GetInstance()->QueueMessage(new CreateAugmentMessage("Vampiric Magnet", col * tileSize.width, row * tileSize.height + tileSize.height));
						layers[i].tiles[unsigned int(mapSize.width * row + col)].triggered = true;
					}
					else if (strcmp(layers[i].tiles[unsigned int(mapSize.width * row + col)].event.c_str(), "SPAWN_DOUBLESHOT_AUGMENT") == 0)
					{
						SGD::MessageManager::GetInstance()->QueueMessage(new CreateAugmentMessage("Double Shot", col * tileSize.width, row * tileSize.height + tileSize.height));
						layers[i].tiles[unsigned int(mapSize.width * row + col)].triggered = true;
					}
					else if (strcmp(layers[i].tiles[unsigned int(mapSize.width * row + col)].event.c_str(), "SPAWN_SHOCKWAVE_AUGMENT") == 0)
					{
						SGD::MessageManager::GetInstance()->QueueMessage(new CreateAugmentMessage("Shockwave", col * tileSize.width, row * tileSize.height + tileSize.height));
						layers[i].tiles[unsigned int(mapSize.width * row + col)].triggered = true;
					}
					else if (strcmp(layers[i].tiles[unsigned int(mapSize.width * row + col)].event.c_str(), "SPAWN_FIREBLAST_AUGMENT") == 0)
					{
						SGD::MessageManager::GetInstance()->QueueMessage(new CreateAugmentMessage("Fire Blast", col * tileSize.width, row * tileSize.height + tileSize.height));
						layers[i].tiles[unsigned int(mapSize.width * row + col)].triggered = true;
					}
					else if (strcmp(layers[i].tiles[unsigned int(mapSize.width * row + col)].event.c_str(), "SPAWN_SOLARFLARE_AUGMENT") == 0)
					{
						SGD::MessageManager::GetInstance()->QueueMessage(new CreateAugmentMessage("Solar Flare", col * tileSize.width, row * tileSize.height + tileSize.height));
						layers[i].tiles[unsigned int(mapSize.width * row + col)].triggered = true;
					}
					else if (strcmp(layers[i].tiles[unsigned int(mapSize.width * row + col)].event.c_str(), "SPAWN_EXPLOSIVE_AUGMENT") == 0)
					{
						SGD::MessageManager::GetInstance()->QueueMessage(new CreateAugmentMessage("Explosive", col * tileSize.width, row * tileSize.height + tileSize.height));
						layers[i].tiles[unsigned int(mapSize.width * row + col)].triggered = true;
					}
					else if (strcmp(layers[i].tiles[unsigned int(mapSize.width * row + col)].event.c_str(), "SPAWN_SEEKERSLUDGE_AUGMENT") == 0)
					{
						SGD::MessageManager::GetInstance()->QueueMessage(new CreateAugmentMessage("Seeker Sludge", col * tileSize.width, row * tileSize.height + tileSize.height));
						layers[i].tiles[unsigned int(mapSize.width * row + col)].triggered = true;
					}
					else if (strcmp(layers[i].tiles[unsigned int(mapSize.width * row + col)].event.c_str(), "SPAWN_MAGICMISSILE_AUGMENT") == 0)
					{
						SGD::MessageManager::GetInstance()->QueueMessage(new CreateAugmentMessage("Magic Missile", col * tileSize.width, row * tileSize.height + tileSize.height));
						layers[i].tiles[unsigned int(mapSize.width * row + col)].triggered = true;
					}
					else if (strcmp(layers[i].tiles[unsigned int(mapSize.width * row + col)].event.c_str(), "SPAWN_BULLETTYPE_AUGMENTTABLE") == 0)
					{
						SGD::MessageManager::GetInstance()->QueueMessage(new CreateWeaponAugmentTable(col * tileSize.width, row * tileSize.height + tileSize.height, 0));
					}
					else if (strcmp(layers[i].tiles[unsigned int(mapSize.width * row + col)].event.c_str(), "SPAWN_SHOTSTYLE_AUGMENTTABLE") == 0)
					{
						SGD::MessageManager::GetInstance()->QueueMessage(new CreateWeaponAugmentTable(col * tileSize.width, row * tileSize.height + tileSize.height, 1));
					}
					else if (strcmp(layers[i].tiles[unsigned int(mapSize.width * row + col)].event.c_str(), "SPAWN_MOVEMENT_AUGMENTTABLE") == 0)
					{
						SGD::MessageManager::GetInstance()->QueueMessage(new CreateWeaponAugmentTable(col * tileSize.width, row * tileSize.height + tileSize.height, 2));\
					}
				}
				else if (strcmp(layers[i].tiles[unsigned int(mapSize.width * row + col)].event.c_str(), "SPAWN_GOBLIN") == 0 ||
						strcmp(layers[i].tiles[unsigned int(mapSize.width * row + col)].event.c_str(), "SPAWN_ELF") == 0 ||
						strcmp(layers[i].tiles[unsigned int(mapSize.width * row + col)].event.c_str(), "SPAWN_BAT") == 0 ||
						strcmp(layers[i].tiles[unsigned int(mapSize.width * row + col)].event.c_str(), "SPAWN_KNIGHT") == 0 ||
						strcmp(layers[i].tiles[unsigned int(mapSize.width * row + col)].event.c_str(), "SPAWN_ARCHER") == 0)
				{
					layers[i].tiles[unsigned int(mapSize.width * row + col)].triggered = false;
				}
			}
		}
	}
}
void Map::Spawn()
{
	SGD::Point cam = Game::GetInstance()->GetCameraPos();
	int screenWidth = int(Game::GetInstance()->GetScreenWidth());
	int screenHeight = int(Game::GetInstance()->GetScreenHeight());
	for (int col = int(cam.x / tileSize.width); col < int((cam.x + screenWidth + 150) / tileSize.width) && col < int(mapSize.width); col++)
	{
		for (int row = int(cam.y / tileSize.height); row < int((cam.y + screenHeight) / tileSize.height) && row < int(mapSize.height); row++)
		{
			for (int i = 0; i < numLayers; i++)
			{
				if (!layers[i].tiles[unsigned int(mapSize.width * row + col)].triggered)
				{
					if (strcmp(layers[i].tiles[unsigned int(mapSize.width * row + col)].event.c_str(), "") == 0 ||
						strcmp(layers[i].tiles[unsigned int(mapSize.width * row + col)].event.c_str(), "NONE") == 0 ||
						strcmp(layers[i].tiles[unsigned int(mapSize.width * row + col)].event.c_str(), "TILECOLLISION") == 0)
					{
						continue;
					}
					else if (strcmp(layers[i].tiles[unsigned int(mapSize.width * row + col)].event.c_str(), "SPAWN_GOBLIN") == 0)
					{
						SGD::MessageManager::GetInstance()->QueueMessage(new CreateGoblinMessage(col * tileSize.width, row * tileSize.height + tileSize.height));
						layers[i].tiles[unsigned int(mapSize.width * row + col)].triggered = true;
					}
					else if (strcmp(layers[i].tiles[unsigned int(mapSize.width * row + col)].event.c_str(), "SPAWN_ELF") == 0)
					{
						SGD::MessageManager::GetInstance()->QueueMessage(new CreateElfMessage(col * tileSize.width, row * tileSize.height + tileSize.height));
						layers[i].tiles[unsigned int(mapSize.width * row + col)].triggered = true;
					}
					else if (strcmp(layers[i].tiles[unsigned int(mapSize.width * row + col)].event.c_str(), "SPAWN_BAT") == 0)
					{
						SGD::MessageManager::GetInstance()->QueueMessage(new CreateBatMessage(col * tileSize.width, row * tileSize.height + tileSize.height));
						layers[i].tiles[unsigned int(mapSize.width * row + col)].triggered = true;
					}
					else if (strcmp(layers[i].tiles[unsigned int(mapSize.width * row + col)].event.c_str(), "SPAWN_KNIGHT") == 0)
					{
						SGD::MessageManager::GetInstance()->QueueMessage(new CreateKnightMessage(col * tileSize.width, row * tileSize.height + tileSize.height));
						layers[i].tiles[unsigned int(mapSize.width * row + col)].triggered = true;
					}
					else if (strcmp(layers[i].tiles[unsigned int(mapSize.width * row + col)].event.c_str(), "SPAWN_ARCHER") == 0)
					{
						SGD::MessageManager::GetInstance()->QueueMessage(new CreateArcherMessage(col * tileSize.width, row * tileSize.height + tileSize.height));
						layers[i].tiles[unsigned int(mapSize.width * row + col)].triggered = true;
					}
				}
			}
		}
	}
}

void Map::BulletDeletion(Projectile* pObject)
{
	if (pObject->GetOwner()->GetType() == MovingObject::OBJ_PLAYER)
	{
		Player* tempunit = dynamic_cast<Player*>(pObject->GetOwner());
		if (tempunit->GetBulletSlot() != nullptr && tempunit->GetBulletSlot()->GetAugName() == "Photon Shot")
		{
			pObject->SetVelocity(-pObject->GetVelocity());
		}
		else
		{
			DestroyGameObjectMessage * Msg = new DestroyGameObjectMessage{ pObject };
			Msg->QueueMessage();
			Msg = nullptr;
			return;
		}
	}
	else
	{
		Enemy* tempunit = dynamic_cast<Enemy*>(pObject->GetOwner());
		if (tempunit->GetAugment()->GetAugName() == "Photon Shot")
		{
			pObject->SetVelocity(-pObject->GetVelocity());
		}
		else
		{
			DestroyGameObjectMessage * Msg = new DestroyGameObjectMessage{ pObject };
			Msg->QueueMessage();
			Msg = nullptr;
			return;
		}
	}
}